﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MeTalker.Aide.data;

namespace MeTalker.Aide.Forms
{
    internal partial class frmCustomRemarks : MeForm
    {
        int pageSize = 20;
        int pageIndex = 0;
        int MaxPage = 0;
        public frmCustomRemarks()
        {
            InitializeComponent();
            txtPageIndex.Text = "1";
            dataGridView.AutoGenerateColumns = false;
            MaxPage = DataFactory.ChatRecords.Records.Count / pageSize;
            if (DataFactory.ChatRecords.Records.Count % pageSize > 0)
            {
                MaxPage += 1;
            }
            BindData(pageIndex);
            txtPageIndex.Text = MaxPage.ToString();
            //var sArray = GetIenumberable<Data>(DataFactory.DataList.Nodes, data => data.DateType == DataType.Remarks, data => data, 1, 1);
            //foreach (var s in sArray)
            //{
            //    Console.WriteLine(s);
            //} 
        }

        /// <summary> 
        /// 分页  
        /// summary> 
        /// <typeparam name="T">typeparam> 
        /// <param name="List">实现IEnumerableparam> 
        /// <param name="FunWhere">delegate检索条件param> 
        /// <param name="FunOrder">delegate排序param> 
        /// <param name="PageSize">每页显示数param> 
        /// <param name="PageIndex">当前页码param> 
        /// <returns>returns> 
        static IEnumerable<T> GetIenumberable<T>(IEnumerable<T> List, Func<T, bool> FunWhere, Func<T, Data> FunOrder, int PageSize, int PageIndex)
        {
            var rance = List.Where(FunWhere).OrderByDescending(FunOrder).
            Select(t => t).Skip((PageIndex - 1) * PageSize).Take(PageSize);
            return rance;
        } 

        void BindData(int PageIndex)
        {

            var d = from temp in DataFactory.DataList.Nodes 
                    where temp.DateType == DataType.Remarks 
                    select temp;
            List<Data> records = d.Skip(pageSize * pageIndex).Take(pageSize).ToList();
            this.dataGridView.DataSource = records;
        }

        private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1) //单元格当然要排除列头
            {
                //也要判断当前点击单元格的名称是否是Link列的名称
                if (this.dataGridView.Columns[e.ColumnIndex].Name == "operate")
                {
                    DataGridViewCell cell = this.dataGridView[2, e.RowIndex];
                    var d = from temp in DataFactory.DataList.Nodes
                            where temp.DateType == DataType.Remarks && temp.Time.Equals(cell.Value.ToString())
                            select temp;
                    DataFactory.DataList.Nodes.Remove(d.FirstOrDefault());
                    BindData(pageIndex);
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDeleteAll_Click(object sender, EventArgs e)
        {
            var ds = from temp in DataFactory.DataList.Nodes
                    where temp.DateType == DataType.Remarks
                    select temp;
            List<Data> lists = ds.ToList();
            foreach (var s in lists)
            {
                //DataFactory.DataList.Nodes.Remove(d.FirstOrDefault());
                DataFactory.DataList.Nodes.Remove(s);
            }
            BindData(0);
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "*.jlso|*.jlso";
            save.FileName = "";
            save.ShowDialog();
            string fileName = save.FileName;
            if (!string.IsNullOrEmpty(fileName))
            {
                SerializerFactory.SerializeFile<DataList>(DataFactory.DataList, fileName);
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "*.jlso|*.jlso";
            open.FileName = "";
            open.ShowDialog();
            string fileName = open.FileName;
            if (!string.IsNullOrEmpty(fileName))
            {
                DataFactory.DataList = SerializerFactory.DeserializeFile<DataList>(fileName) as DataList;
                if (DataFactory.DataList != null)
                    this.BindData(0);
            }

        }

        private void btnPreviousPage_Click(object sender, EventArgs e)
        {
            if (pageIndex > 0)
                BindData(pageIndex-1);

        }

        private void btnNextPage_Click(object sender, EventArgs e)
        {
            if (MaxPage <= pageIndex)
                BindData(pageIndex+1);
        }

        private void btnPageIndex_Click(object sender, EventArgs e)
        {
            int index;
            int.TryParse(txtPageIndex.Text, out index);
            if (MaxPage <= index && index > 0)
                BindData(index - 1);
        }
    }
}
